package com.survey.service.impl;

import com.github.pagehelper.PageHelper;
import com.survey.dao.mapper.AdminMapper;
import com.survey.pojo.data.AdminDO;
import com.survey.service.AdminService;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import com.survey.pojo.vo.AdminVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/** 
 * <br/>
 * Created by survey on 2018/08/03
 */
@Service("adminService")
public class AdminServiceImpl implements AdminService {

	private final static Logger LOG = LoggerFactory.getLogger(AdminServiceImpl.class);

	@Resource
	private AdminMapper adminMapper;

	@Override
	public Result addAdmin(AdminVO adminVO) throws Exception {
		Integer row = adminMapper.addAdmin(adminVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS,"添加成功");
		}
		return new Result(Result.CODE_FAILURE,"添加失败");
	}

	@Override
	public Result updateAdmin(AdminVO adminVO) throws Exception {
		Integer row = adminMapper.updateAdmin(adminVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS,"修改成功");
		}
		return new Result(Result.CODE_SUCCESS,"修改失败");
	}

	@Override
	public AdminDO findDetailAdmin(AdminVO adminVO) throws Exception{
		return adminMapper.findDetailAdmin(adminVO);
	}



	@Override
	public List<AdminDO> listAdmin(AdminVO adminVO) throws Exception{
		return adminMapper.listAdmin(adminVO);
	}


	@Override
	public Result listAdminPage(AdminVO adminVO) throws Exception{
		return null;
	}
	
	@Override
	public Result countAdmin(AdminVO adminVO) throws Exception{
		return null;
	}
	
	@Override
	public Result deleteAdmin(AdminVO adminVO) throws Exception{
		Integer row = adminMapper.deleteAdmin(adminVO);
		if(row>0){
			return new Result(Result.CODE_SUCCESS);
		}
		else{
			return new Result(Result.CODE_FAILURE);
		}
	}

	@Override
	public PageBean<AdminDO> adminListManager(AdminVO query) {
		//页码越界
		//获取数据的总个数
		int count = adminMapper.countAdmin(query);
		//计算最大页数
		int max = (count + query.getPageSize() - 1) / query.getPageSize();
		if(max<0){
			return null;
		}
		if (query.getPageCode() > max) {
			return null;
		}
		PageHelper.startPage(query.getPageCode(), query.getPageSize());
		List<AdminDO> adminDOS = adminMapper.adminListManager(query);
		//根据插件提供的分页信息，构造我们自己的分页信息
		PageBean<AdminDO> pageBean = new PageBean<AdminDO>(adminDOS,query.getPageSize()
				,query.getPageCode(),count,query.getSize());
		System.out.println(adminDOS);
		return pageBean;
	}

}